package com.harvest.scientific.ladder.page

import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.harvest.scientific.ladder.R
import com.harvest.scientific.ladder.ext.toJson
import com.harvest.scientific.ladder.net.ComParams
import com.harvest.scientific.ladder.net.ReRsitory
import com.harvest.scientific.ladder.net.request
import com.harvest.scientific.ladder.net.toRespObj
import com.harvest.scientific.ladder.res.AdmireMoreAcheRes
import com.harvest.scientific.ladder.res.CombNorthDrawerRes
import com.harvest.scientific.ladder.res.FleeSocialistExpeditionRes
import com.harvest.scientific.ladder.res.JsonBigBean
import com.harvest.scientific.ladder.res.RegardMostBeehiveRes
import com.harvest.scientific.ladder.util.AesssUtil
import com.harvest.scientific.ladder.util.ApUtil
import com.harvest.scientific.ladder.util.AppGlobals
import com.harvest.scientific.ladder.util.BigJsonUtil
import com.harvest.scientific.ladder.util.LogUtils
import com.harvest.scientific.ladder.util.SpUtil
import com.harvest.scientific.ladder.util.ToastUtil
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File

class AppModel : ViewModel() {

    private val TAG = "AppModel"
    private val sitory = ReRsitory()

    val loginSuc by lazy { MutableLiveData<Boolean> () }
    val provinceList by lazy { MutableLiveData<List<RegardMostBeehiveRes>>() }
    val cityList by lazy { MutableLiveData<List<RegardMostBeehiveRes>>() }
    val areaList by lazy { MutableLiveData<List<RegardMostBeehiveRes>>() }
    val sexSuc by lazy { MutableLiveData<String> () }
    val searchInfo1 by lazy { MutableLiveData<FleeSocialistExpeditionRes?>() }
    val saveInfo1Suc by lazy { MutableLiveData<Boolean> ()  }
    val relationshipSuc by lazy { MutableLiveData<String> () }
    val secRelationshipSuc by lazy { MutableLiveData<String> () }
    val searchInfo2 by lazy { MutableLiveData<FleeSocialistExpeditionRes?>() }
    val saveInfo2Suc by lazy { MutableLiveData<Boolean>()  }
    val collectionWaySuc by lazy { MutableLiveData<String>() }
    val delayViolentTheirSuc by lazy { MutableLiveData<Pair<Boolean,String>>() }
    val admireMoreAcheRes by lazy { MutableLiveData<AdmireMoreAcheRes?>() }
    val saveInfo3Suc by lazy { MutableLiveData<Boolean>()  }
    val searchInfo3 by lazy { MutableLiveData<FleeSocialistExpeditionRes?>() }
    val searchInfo4 by lazy { MutableLiveData<CombNorthDrawerRes?>() }
    val saveInfo4Suc by lazy { MutableLiveData<Boolean> ()  }
    val showLoading by lazy { MutableLiveData<Boolean> () }

    fun delayViolentTheir(
        crazyShaverStrengthTyre: String,
        file: File
    ){
        request(requestBlock = {
            sitory.delayViolentTheir(crazyShaverStrengthTyre,file)
        }, callBack = {
            onSuccess = {
                Log.d(TAG,"delayViolentTheir value:${it}")
                delayViolentTheirSuc.postValue(Pair(true,crazyShaverStrengthTyre))
            }
            onFailed = { code, msg ->
                Log.d(TAG,"delayViolentTheir code:${code} msg:${msg}")
                delayViolentTheirSuc.postValue(Pair(false,msg?: ""))
            }
        })
    }


    /**
     * sms
     */
    fun developBothTraining(uselessElectricAnimal: String?) {
        request(requestBlock = {
            sitory.developBothTraining(uselessElectricAnimal)
        }, callBack = {
            onSuccess = {
                Log.d(TAG,"developBothTraining value:${it?.moreStillThemeArmy}")
            }
            onFailed = { code, msg ->
                Log.d(TAG,"code:${code} msg:${msg}")
            }
        })

    }

    /**
     * login
     */
    fun supportGreyMember(
        uselessElectricAnimal: String,
        moreStillThemeArmy:String
    ){
        showLoading.postValue(true)
        request(requestBlock = {
            sitory.supportGreyMember(uselessElectricAnimal, moreStillThemeArmy)
        }, callBack = {
            onSuccess = {
                Log.d(TAG,"supportGreyMember")
                SpUtil.put(SpUtil.KYE_USER_ID, it?.freezingSaleswomanFurnishedDuckling ?: "")
                SpUtil.put(SpUtil.KYE_USER_TOKE, it?.naturalPardonSmallBuddhism ?: "")
                SpUtil.put(SpUtil.KYE_USER_PHONE, uselessElectricAnimal ?: "")
                SpUtil.put(SpUtil.KYE_USER_TEST_CUST_FLAG, it?.maximumPassivePie ?: "")
                SpUtil.put(SpUtil.KYE_USER_NEW_CUST_FLAG, it?.betterHandsomeFriendPint ?: "")
                loginSuc.postValue(true)
            }
            onFailed = { code, msg ->
                Log.d(TAG,"code:${code} msg:${msg}")
                ToastUtil.showToast(msg)
                loginSuc.postValue(false)
            }
            onComplete = {
                showLoading.postValue(false)
            }
        })

    }


    fun tasteCottonDea(

    ){
        request(requestBlock = {
            sitory.tasteCottonDeal()
        }, callBack = {
            onSuccess = {
                Log.d(TAG,"fleeSocialistExpedition")
                loginSuc.postValue(true)
            }
            onFailed = { code, msg ->
                Log.d(TAG,"code:${code} msg:${msg}")
                loginSuc.postValue(false)
            }
        })
    }

    /**
     * search info1 1
     * search info2 3
     */
    fun fleeSocialistExpedition(
        mexicanRegardsMistakenRoofInterestingGallery:Int
    ){
        showLoading.postValue(true)
        request(requestBlock = {
            sitory.fleeSocialistExpedition(mexicanRegardsMistakenRoofInterestingGallery)
        }, callBack = {
            onSuccess = {
                Log.d(TAG,"fleeSocialistExpedition")
                if (mexicanRegardsMistakenRoofInterestingGallery == 1) {
                    searchInfo1.postValue(it)
                } else if (mexicanRegardsMistakenRoofInterestingGallery == 3) {
                    searchInfo2.postValue(it)
                } else if (mexicanRegardsMistakenRoofInterestingGallery == 4) {
                    searchInfo3.postValue(it)
                }
            }
            onFailed = { code, msg ->
                Log.d(TAG,"code:${code} msg:${msg}")
            }
             onComplete = {
                 showLoading.postValue(false)
             }
        })
    }

    /**
     * search type
     */
    fun rememberBlankAnt(
        crazyShaverStrengthTyre: String
    ){
        request(requestBlock = {
            sitory.rememberBlankAnt(crazyShaverStrengthTyre)
        }, callBack = {
            onSuccess = {
                LogUtils.d(TAG,"${it}")
                if (crazyShaverStrengthTyre == "sex") {
                    sexSuc.postValue(it?.toJson())
                } else if (crazyShaverStrengthTyre == "relationship") {
                    relationshipSuc.postValue(it.toJson())
                } else if (crazyShaverStrengthTyre == "secRelationship") {
                    secRelationshipSuc.postValue(it.toJson())
                } else if (crazyShaverStrengthTyre == "collectionType") {
                    collectionWaySuc.postValue(it.toJson())
                }
            }
            onFailed = { code, msg ->
                LogUtils.d(TAG,"rememberBlankAnt crazyShaverStrengthTyre:${crazyShaverStrengthTyre} code:${code} msg:${msg}")
            }
        })
    }

    /**
     * get province
     */
    fun regardMostBeehive(
        femaleRecorderDrum:String = "-1",
        troublesomeDisappointmentHugePostcodeReasonableSpade: String = "1"
    ) {
        request(requestBlock = {
            sitory.regardMostBeehive(femaleRecorderDrum, troublesomeDisappointmentHugePostcodeReasonableSpade)
        }, callBack = {
            onSuccess = { list ->
                Log.d(TAG,"fleeSocialistExpedition")
                when(troublesomeDisappointmentHugePostcodeReasonableSpade) {
                    "1" -> {
                        provinceList.postValue(list)
                        if (!list.isNullOrEmpty()) {
                            regardMostBeehive(list[0].unhealthyCampAmericanLightning ?: "", "2")
                        }
                    }
                    "2" -> {
                        cityList.postValue(list)
                        if (!list.isNullOrEmpty()) {
                            regardMostBeehive(list[0].unhealthyCampAmericanLightning ?: "", "3")
                        }
                    }
                    "3" -> {
                        areaList.postValue(list)
                    }
                }
            }
            onFailed = { code, msg ->
                Log.d(TAG,"code:${code} msg:${msg}")
                loginSuc.postValue(false)
            }
        })
    }

    /**
     * save info1 1
     */
    fun realiseArabPrescription1(
        mexicanRegardsMistakenRoofInterestingGallery: Int,
        festivalPaper: String,
        suddenGasTwin: String,
        unsuccessfulFavouriteEmbassy: String,
        southRecordSureSleeve: String,
        actualStreamColdVinegar: String,
        furnishedSink: String,
        indeedSoccerUsefulGuest: String,
        greyShineContributionCar: String,
        solidThereLake: String,
        roundExerciseTroublesomeStranger: String,
        thinFriendCoolTastelessQuilt: String,
        allInternetSailingAgriculturalTiger: String,
        rapidNobleBestBacon: String,
        considerateHungerTomato: String,
    ) {
        request(requestBlock = {
            sitory.realiseArabPrescription1(
                mexicanRegardsMistakenRoofInterestingGallery,
                festivalPaper,
                suddenGasTwin,
                unsuccessfulFavouriteEmbassy,
                southRecordSureSleeve,
                actualStreamColdVinegar,
                furnishedSink,
                indeedSoccerUsefulGuest,
                greyShineContributionCar,
                solidThereLake,
                roundExerciseTroublesomeStranger,
                thinFriendCoolTastelessQuilt,
                allInternetSailingAgriculturalTiger,
                rapidNobleBestBacon,
                considerateHungerTomato
            )
        }, callBack = {
            onSuccess = {
                Log.d(TAG,"fleeSocialistExpedition")
                saveInfo1Suc.postValue(true)
            }
            onFailed = { code, msg ->
                Log.d(TAG,"code:${code} msg:${msg}")
                saveInfo1Suc.postValue(false)
                ToastUtil.showToast(msg)
            }
        })
    }

    /**
     * info2
     */
    fun realiseArabPrescription2(
        mexicanRegardsMistakenRoofInterestingGallery: Int,
        standardInternationalCrossroads: String,
        sleepyElectricBotanySatellite: String,
        compressedAnybodyPyramid: String,
        usedSuccess: String,
        mistakenPlayroomHardworkingPorterSalesgirl: String,
        afraidQuarterShow: String,
    ) {
        request(requestBlock = {
            sitory.realiseArabPrescription2(
                mexicanRegardsMistakenRoofInterestingGallery,
                standardInternationalCrossroads,
                sleepyElectricBotanySatellite,
                compressedAnybodyPyramid,
                usedSuccess,
                mistakenPlayroomHardworkingPorterSalesgirl,
                afraidQuarterShow
            )
        }, callBack = {
            onSuccess = {
                Log.d(TAG,"fleeSocialistExpedition")
                saveInfo2Suc.postValue(true)
            }
            onFailed = { code, msg ->
                Log.d(TAG,"code:${code} msg:${msg}")
                saveInfo2Suc.postValue(false)
                ToastUtil.showToast(msg)
            }
        })
    }

    /**
     * info3
     */
    fun realiseArabPrescription3(
        mexicanRegardsMistakenRoofInterestingGallery: Int,
        terminalNeckHomelandPartner: String,
    ) {
        request(requestBlock = {
            sitory.realiseArabPrescription3(
                mexicanRegardsMistakenRoofInterestingGallery,
                terminalNeckHomelandPartner,
            )
        }, callBack = {
            onSuccess = {
                Log.d(TAG,"realiseArabPrescription3")
                saveInfo3Suc.postValue(true)
            }
            onFailed = { code, msg ->
                Log.d(TAG,"code:${code} msg:${msg}")
                saveInfo3Suc.postValue(false)
                ToastUtil.showToast(msg)
            }
        })
    }

    fun admireMoreAche(
        mexicanRegardsMistakenRoofInterestingGallery: String
    ) {
        request(requestBlock = {
            sitory.admireMoreAche(mexicanRegardsMistakenRoofInterestingGallery)
        }, callBack = {
            onSuccess = {
                LogUtils.d(TAG,"admireMoreAche")
                admireMoreAcheRes.postValue(it)
            }
            onFailed = { code, msg ->
                LogUtils.d(TAG,"admireMoreAche code:${code} msg:${msg}")
                admireMoreAcheRes.postValue(null)
            }
        })
    }

    fun combNorthDrawer(
        mexicanRegardsMistakenRoofInterestingGallery: String
    ){
        showLoading.postValue(true)
        request(requestBlock = {
            sitory.combNorthDrawer(mexicanRegardsMistakenRoofInterestingGallery)
        }, callBack = {
            onSuccess = {
                LogUtils.d(TAG,"combNorthDrawer")
                if (it.isNullOrEmpty()) {
                    searchInfo4.postValue(null)
                } else {
                    searchInfo4.postValue(it.first())
                }
            }
            onFailed = { code, msg ->
                LogUtils.d(TAG,"combNorthDrawer code:${code} msg:${msg}")
            }
            onComplete = {
                showLoading.postValue(false)
            }
        })
    }

    fun uniteDangerousMineral(
        anotherSkillfulPatternBathtub: String,
        technicalBigMealLoad:String,
        nationalStandNoisyClothSecondhand: String
    ){
        request(requestBlock = {
            sitory.uniteDangerousMineral(
                anotherSkillfulPatternBathtub,
                technicalBigMealLoad,
                nationalStandNoisyClothSecondhand
            )
        }, callBack = {
            onSuccess = {
                LogUtils.d(TAG,"${it}")
                saveInfo4Suc.postValue(true)
            }
            onFailed = { code, msg ->
                LogUtils.d(TAG,"uniteDangerousMineral code:${code} msg:${msg}")
                saveInfo4Suc.postValue(false)
                ToastUtil.showToast(msg)
            }
        })
    }

    fun greetMerryRabbit() {
        val bean = JsonBigBean()
        bean.illPetrolMainland = ApUtil.getVersionCode()
        bean.closeFurFoolishEachPipe = ApUtil.getVersionName()
        bean.roughAirlineFogDriver = AppGlobals.get().packageName
        bean.clearBattlegroundChart = System.currentTimeMillis().toString()
        val bodyStr = AesssUtil.aesData(BigJsonUtil.compressJson(bean.toJson()), AesssUtil.KEY)
        LogUtils.d("greetMerryRabbit","bodyStr:${bodyStr}")
        request(requestBlock = {
            sitory.greetMerryRabbit(
                bodyStr
            )
        }, callBack = {
            onSuccess = {
                LogUtils.d(TAG,"${it}")

            }
            onFailed = { code, msg ->
                LogUtils.d(TAG,"uniteDangerousMineral code:${code} msg:${msg}")
                ToastUtil.showToast(msg)
            }
        })
    }


}